package org.jetlinks.community.standalone.entry;

import lombok.Data;
import org.hswebframework.web.api.crud.entity.PagerResult;
import org.jetlinks.community.device.entity.DeviceInstanceEntity;
import org.jetlinks.community.device.entity.DeviceProductEntity;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.stream.Collectors;

@Data
public class ThingsJsDeviceResultDto {

    private String classname;

    private List<ThingsJsDeviceDto> list;

    private Integer pageNum;
    private Integer pageSize;
    private Integer totalCount;

    public Mono<ThingsJsDeviceResultDto> with(DeviceProductEntity deviceProduct, PagerResult<DeviceInstanceEntity> page) {
        return Mono.zip(Mono.just(deviceProduct), Mono.just(page))
            .doOnNext(tp -> {
                setClassname(tp.getT1().getName());
                setPageNum(tp.getT2().getPageIndex());
                setPageSize(tp.getT2().getPageSize());
                setTotalCount(tp.getT2().getTotal());
                setList(tp.getT2().getData().stream()
                            .map(deviceInstance ->
                                new ThingsJsDeviceDto().with(deviceProduct, deviceInstance))
                            .collect(Collectors.toList()));
            }).thenReturn(this);
    }

    public Mono<ThingsJsDeviceResultDto> with(PagerResult<DeviceInstanceEntity> page) {
        return  Mono.just(page)
            .doOnNext(tp -> {
                setPageNum(tp.getPageIndex());
                setPageSize(tp.getPageSize());
                setTotalCount(tp.getTotal());
                setList(tp.getData().stream()
                    .map(deviceInstance ->
                        new ThingsJsDeviceDto().with(deviceInstance))
                    .collect(Collectors.toList()));
            }).thenReturn(this);
    }
}
